(function () {

    var inputs = document.getElementsByClassName("input"),
        id,
        hint = document.getElementsByClassName("hint"),
        err = 3;


    var hintText = {
        user_email: { hint: "⚠️邮箱是您登录的唯一账号，请谨慎填写", right: "√邮箱格式正确", wrong: "×邮箱格式有误，请重新输入" },
        user_name: { hint: "⚠️请输入3-12个字符的用户名（包括字母/数字/下划线）", right: "√用户名格式正确", wrong: "×用户名格式有误，请重新输入" },
        name: { hint: "⚠️请输入3-12姓名", right: "√姓名输入正确", wrong: "×姓名输入有误，请重新输入" },
        address: { hint: "⚠️请输入正确地址", right: "√地址输入正确", wrong: "×地址输入有误，请重新输入" },
        weight: { hint: "⚠️请输入物品质量", right: "√物品质量已输入输入", wrong: "×物品质量输入有误，请重新输入" },
        phone: { hint: "⚠️请输入11位电话号码", right: "√电话号码输入正确", wrong: "×电话号码输入有误，请重新输入" },
        id_card: { hint: "⚠️请输入18位身份证号码", right: "√身份证号码输入正确", wrong: "×身份证号码输入有误，请重新输入" },
        password: { hint: "⚠️请输入6位以上密码", right: "√密码格式正确", wrong: "×请输入符合格式的密码" },
        repassword: { hint: "⚠️请再次输入密码", right: "√再次输入密码正确", wrong: "×两次输入不一致或密码格式不正确，请重新输入或密码格式不正确" }
    };

    function regEvent(node, event, func) {
        if (node.addEventListener)
            node.addEventListener(event, func);
        else if (node.attachEvent)
            node.attachEvent("on" + event, func);
        else
            node["on" + event] = func;
    };


    function regValue(id, i) {
        var flag = false,
            input = document.getElementById(id),
            value = input.value;
        switch (id) {
            case "user_name":
            case "login_user_name":
            case "info_user_name":
                flag = /^[a-zA-Z0-9_]{4,16}$/.test(value.replace(/[\u0391-\uFFE5]/g, "nn"));
                id = "user_name";
                break;
            case "name":
            case "send_to_name":
            case "send_from_name":
                flag = /^[a-zA-Z ]{1,20}$/.test(value.replace(/[\u0391-\uFFE5]/g, "nn"));
                id = "name";
                break;
            case "send_from_address":
            case "send_to_address":
                flag = /^\S{6,16}$/.test(value.replace(/[\u0391-\uFFE5]/g, "nn"));
                id = "address";
                break;
            case "password":
            case "login_password":
            case "info_password":
                flag = /^\S{6,16}$/.test(value);
                id = "password";
                break;
            case "repassword":
                flag = document.getElementById("password").value == value && value != "" && value != null && (/^\S{6,16}$/.test(value));
                break;
            case "info_repassword":
                flag = document.getElementById("info_password").value == value && value != "" && value != null && (/^\S{6,16}$/.test(value));
                id = "repassword";
                break;
            case "user_email":
            case "forget_user_email":
            case "info_user_email":
                flag = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}/.test(value);
                id = "user_email";
                break;
            case "phone":
            case "info_phone":
            case "send_to_phone":
            case "send_from_phone":
                flag = /^((\(\d{2,3}\))|(\d{3}\-))?1[3,8,5]{1}\d{9}$/.test(value);
                id = "phone";
                break;
            case "id_card":
                flag = /^\S{18}$/.test(value);
                break;
            case "weight":
                flag = /^\d{1,4}$/.test(value);
                break;
            default:
                break;
        }

        if (flag) {
            err--;
            input.className = "right input";
            hint[i].className = "hint hint_right";
            hint[i].innerHTML = hintText[id].right;
        } else {
            input.className = "wrong input";
            hint[i].className = "hint hint_wrong";
            hint[i].innerHTML = hintText[id].wrong;
        }
    };



    for (var j = 0; j < inputs.length; j++) {
        (function (i) {
            regEvent(inputs[i], "focus", function () {
                hint[i].style.visibility = "visible";
                id = inputs[i].id;
            });
            regEvent(inputs[i], "blur", function () {
                regValue(id, i);
            });
        })(j)
    }

    let code=location.href.substring(location.href.lastIndexOf('/') + 1);
    // 注册
    $('#register').on('click', () => {

        if ($('.hint_wrong').length === 0) {
            let username = $('#user_name').val();
            let password = $('#password').val();
            let confimpwd = $('#repassword').val();

            axios.post('/api/user/register', {
                username,
                password,
                confimpwd
            }).then(res => {
                res = res.data;
                if (res.code === 200) {
                    alert(res.msg);
                    signin();

                } else {
                    alert(res.err);

                }
            }).catch(err => {
                console.log(err);
            })

        }

    })

    // 登录
    $('#login').on('click', () => {
        console.log($('.hint_wrong').length);
        if ($('.hint_wrong').length === 0) {
            let username = $('#login_user_name').val();
            let password = $('#login_password').val();

            axios.post('/api/user/login', {
                username,
                password
            }).then(res => {
                res = res.data;
                console.log(res);
                if (res.code === 200) {
                    if (res.id === 1) {
                        location.href = '/'
                    } else {
                        console.log(code);
                        location.href=`/quest/scorewj/${code}`
                    }

                } else {
                    alert(res.err);
                }
            }).catch(err => {
                console.log(err);
            })
        }

    })



})();



function signin() {
    console.log('点击了登录');
    this.className = "signin focus";
    $(".signup")[0].className = "signup";
    $(".input_signin")[0].className = "input_signin active";
    $(".input_signup")[0].className = "input_signup";
}

function signup() {
    console.log('点击了注册');
    this.className = "signup focus";
    $(".signin")[0].className = "signin";
    $(".input_signup")[0].className = "input_signup active";
    $(".input_signin")[0].className = "input_signin";
}